package cn.wendellup.bootStrap.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.wendellup.bootStrap.service.contract.IAdvService;
import cn.wendellup.bootStrap.service.contract.IAppParameterService;
import cn.wendellup.bootStrap.service.contract.IFileService;
import cn.wendellup.bootStrap.service.contract.IUserService;
import cn.wendellup.bootStrap.valueobject.entity.AdvDetail;
import cn.wendellup.bootStrap.valueobject.entity.AdvPosition;
import cn.wendellup.bootStrap.valueobject.entity.AppParameter;
import cn.wendellup.bootStrap.valueobject.vo.AdvDetailVo;
import cn.wendellup.bootStrap.valueobject.vo.AdvsInfoVo;

@Controller
@RequestMapping(value="/")
public class IndexController {
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IAdvService advService;
	
	@Autowired
	private IAppParameterService appParameterService;
	
	@Autowired
	private IFileService fileServiceImpl;
	
	@RequestMapping(value = "/listChannel")
	public Object listChannel(HttpServletRequest request, HttpServletResponse
			response) throws ExceptionCommonBase{
		int parentId = Utils.toInt(request.getParameter("parent_id"), 101);
		int channelId = Utils.toInt(request.getParameter("channel_id"), 101);
		
		//导航信息
		List<Integer> appParameterIds = appParameterService.listAppParameterIdsByParentId(parentId);
		List<AppParameter> appParameters = new ArrayList<AppParameter>();
		for(Integer id : appParameterIds){
			AppParameter app = appParameterService.getAppParameterById(id);
			appParameters.add(app);
		}
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("listChannel", appParameters);
		
		//展现的广告信息
		List<AdvsInfoVo> advsInfoVos = new ArrayList<AdvsInfoVo>();
		List<Integer> advPositionIds = advService.listAdvPositionIdByAppId(channelId);
		for(Integer advPositionId : advPositionIds){
			AdvPosition advPosition = advService.getAdvPositionById(advPositionId);
			List<Integer> advDetailIds = advService.listAdvDetailIdsByAdvPostionId(advPositionId);
			List<AdvDetailVo> advDetailVos = new ArrayList<AdvDetailVo>();
			for(Integer advDetailId : advDetailIds){
				AdvDetailVo advDetailVo = 
						new AdvDetailVo(advService.getAdvDetailById(advDetailId), fileServiceImpl);
				
				advDetailVos.add(advDetailVo);
			}
			AdvsInfoVo advsInfoVo = new AdvsInfoVo();
			advsInfoVo.setAdv_layout(advPosition.getTypeId());
			advsInfoVo.setAdv_position_id(advPosition.getId());
			advsInfoVo.setDetail_list(advDetailVos);
			advsInfoVos.add(advsInfoVo);
		}
		model.put("advsInfoVos", advsInfoVos);
		
		ModelAndView mav = new ModelAndView("index/listChannel",model);
		return mav;
	}
}
